package com.css.zfzx.fxpg.modules.casulaty.offline.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fxpg.modules.casulaty.offline.repository.entity.FxCasulatyOfflineEntity;
import com.css.zfzx.fxpg.modules.casulaty.offline.service.FxCasulatyOfflineService;
import com.css.zfzx.fxpg.modules.casulaty.offline.viewobjects.FxCasulatyOfflineQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人员伤亡损失线下评估
 *
 * @author leon
 * @date 2020-11-16
 */
@Slf4j
@RestController
@RequestMapping("/fxCasulatyOfflines")
public class FxCasulatyOfflineController {
    @Autowired
    private FxCasulatyOfflineService fxCasulatyOfflineService;

    @GetMapping("/queryFxCasulatyOfflines")
    public RestResponse queryFxCasulatyOfflines(HttpServletRequest request, FxCasulatyOfflineQueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxCasulatyOfflineService.queryFxCasulatyOfflines(queryParams, curPage, pageSize);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getFxCasulatyOffline(@PathVariable String id) {
        RestResponse response = null;
        try {
            FxCasulatyOfflineEntity fxCasulatyOffline = fxCasulatyOfflineService.getFxCasulatyOffline(id);
            response = RestResponse.succeed(fxCasulatyOffline);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping
    public RestResponse saveFxCasulatyOffline(@RequestBody FxCasulatyOfflineEntity fxCasulatyOffline) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxCasulatyOfflineService.saveFxCasulatyOffline(fxCasulatyOffline);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    @PutMapping
    public RestResponse updateFxCasulatyOffline(@RequestBody FxCasulatyOfflineEntity fxCasulatyOffline) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxCasulatyOfflineService.updateFxCasulatyOffline(fxCasulatyOffline);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @DeleteMapping
    public RestResponse deleteFxCasulatyOfflines(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxCasulatyOfflineService.deleteFxCasulatyOfflines(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxCasulatyOfflineService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

}